package com.itbaizhan.rocketmqdemo.consumer;

import com.sun.javafx.scene.traversal.SceneTraversalEngine;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;
//延迟消息消费者
public class DelayMessageConsumer {
    public static void main(String[] args) throws MQClientException {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("transactionConsumer-grp-01");
        consumer.setNamesrvAddr("192.168.66.100:9876");  //指定namesrv地址
        //设置消息重度次数
        consumer.setMaxReconsumeTimes(5);
        consumer.setConsumeMessageBatchMaxSize(1);  //消息消费的批量
        consumer.subscribe("topic_demo_01","*");

        //监听
        consumer.setMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                System.out.println(System.currentTimeMillis()/1000);  //System.currentTimeMillis() 当前的毫秒数
                for(MessageExt msg:list){
                    System.out.println(msg.getTopic()+"\t"+
                                       msg.getQueueId()+"\t"+
                                        msg.getDelayTimeLevel()+"\t"+
                                        new String(msg.getBody()));
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        consumer.start();
    }
}
